package com.app.repository.impl;

import com.app.common.enums.DataStatusEnum;
import com.app.common.enums.FrontlineEnum;
import com.app.entity.Goods;
import com.app.repository.IGoodsRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @autor xiaoyu.fang
 * @date 2019/10/11 16:37
 */
@Component
public class IGoodsRepositoryImpl implements IGoodsRepository<Goods, ObjectId> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Goods> queryTop3Frontline(Integer type) {
        Query query = new Query(Criteria.where("dataStatus").is(DataStatusEnum.VALID.KEY));
        Sort sort = null;
        switch (FrontlineEnum.getByKey(type)) {
            case FASHION:
                sort = new Sort(Sort.Direction.DESC, "inventory");
                break;
            case NEW:
                sort = new Sort(Sort.Direction.DESC, "createAt");
                break;
            case CHOICENESS:
                sort = new Sort(Sort.Direction.DESC, "collection");
                break;
        }
        query.with(sort).limit(3);
        return mongoTemplate.find(query, Goods.class);
    }
}
